package com.miqtech.master.wyrecorder.nim;

import android.content.Context;
import android.text.TextUtils;

import com.miqtech.master.wyrecorder.WYRecorderApp;
import com.miqtech.master.wyrecorder.entity.CustomAttachParser;
import com.miqtech.master.wyrecorder.entity.User;
import com.miqtech.master.wyrecorder.user.UserProxy;
import com.miqtech.master.wyrecorder.utils.DeviceUtils;
import com.miqtech.master.wyrecorder.utils.L;
import com.miqtech.master.wyrecorder.utils.Utils;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.chatroom.ChatRoomService;
import com.netease.nimlib.sdk.chatroom.ChatRoomServiceObserver;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomKickOutEvent;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMessage;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomData;
import com.netease.nimlib.sdk.msg.MsgService;

import java.util.List;


/**
 * Created by arvin on 2016/10/13.
 * 云信
 */

public class NIMHelper {
    private static final String TAG = "NIMHelper";
    private static volatile NIMHelper mInstance;

    private NIMHelper() {
    }

    public static NIMHelper getInstance() {
        if (mInstance == null) {
            synchronized (NIMHelper.class) {
                if (mInstance == null) {
                    mInstance = new NIMHelper();
                }
            }
        }
        return mInstance;
    }

    public void init() {
        Context context = WYRecorderApp.getContext();
        NIMClient.init(context, getLoginInfo(), null);
        if (Utils.inMainProcess()) {
            NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser());
        }
    }

    private LoginInfo getLoginInfo() {
        Context context = WYRecorderApp.getContext();
        User user = UserProxy.getUser();
        if (user != null) {
            String id = user.getShouboAccid();
            String token = user.getId();
            if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(token)) {
                return new LoginInfo(id, token);
            } else {
                String deviceId = DeviceUtils.getDeviceId(context);
                return new LoginInfo(deviceId, deviceId);
            }
        }
        return null;
    }

    private RequestCallback mCallback = new RequestCallback() {
        @Override
        public void onSuccess(Object o) {
            L.e(TAG, "----nim onSuccess----");
        }

        @Override
        public void onFailed(int i) {
            L.e(TAG, "----nim onFailed----");

        }

        @Override
        public void onException(Throwable throwable) {
            L.e(TAG, "----nim onException----");
        }
    };


    public void login(String id, String token) {
        LoginInfo info = new LoginInfo(id, token);
        NIMClient.getService(AuthService.class).login(info).setCallback(mCallback);
    }
    public void logout() {
        NIMClient.getService(AuthService.class).logout();
    }

    /**
     * 聊天室接收消息
     *
     * @param observer
     * @param register true 为注册 false 为注销
     */
    public void receiveMessage(Observer<List<ChatRoomMessage>> observer, boolean register) {
        NIMClient.getService(ChatRoomServiceObserver.class).observeReceiveMessage(observer, register);
    }

    /**
     * 聊天室发送消息
     */
    public <T extends ChatRoomMessage> void sendMessage(T message) {
        NIMClient.getService(ChatRoomService.class).sendMessage(message, true);
    }

    /**
     * 进入聊天室
     */
    public void enterChatRoom(EnterChatRoomData data, RequestCallback callback) {
        NIMClient.getService(ChatRoomService.class).enterChatRoom(data).setCallback(callback);
    }

    /**
     * 注册/注销聊天室消息接收监听回调
     *
     * @param messages 消息接收者
     * @param register true 注册 false 注销
     */
    public void registeMessageObserver(Observer<List<ChatRoomMessage>> messages, boolean register) {
        NIMClient.getService(ChatRoomServiceObserver.class).observeReceiveMessage(messages, register);
    }

    /**
     * 监听被提出聊天室
     *
     * @param observer 观察者, 参数为被踢出的事件（可以获取被踢出的聊天室ID和被踢出的原因）
     * @param register true为注册，false为注销
     */
    public void registeKickOutObserver(Observer<ChatRoomKickOutEvent> observer, boolean register) {
        NIMClient.getService(ChatRoomServiceObserver.class)
                .observeKickOutEvent(observer, register);
    }
}
